<?php
$MDOR = 'ED';
if(isset($_POST['RID'])) {
	$OBJCTRC = $_POST['RID'];
} elseif(isset($_GET['RID'])) {
	$OBJCTRC = $_GET['RID'];
}
$GETCTINFO = $GLOBALS['MYT']->db_array("SELECT
					BnfPais,
					BnfCiudad,
					COUNT(DISTINCT OFId) AS OFCount
			FROM beneficiarios
				INNER JOIN hd_detalletarifas ON dtr_Pais = BnfPais AND dtr_Tarifa = 0
				INNER JOIN corresponsales ON dtr_Corresponsal = CorrNumero
				INNER JOIN ciudades ON CiuPais = BnfPais
				LEFT OUTER JOIN oficcorresp ON OFIDCiudad = CiuNumero AND OFCorr = CorrNumero AND OFActiva = 1
				WHERE BnfNumero = {$OBJCTRC}
				GROUP BY BnfPais");
$SearchConditions = '';
$RSGET = '';
if (!isset($_GET['vr1']) && isset($GETCTINFO[1]['BnfCiudad'])) {
	$SearchConditions .= 'AND OFIDCiudad = '.$GETCTINFO[1]['BnfCiudad'];
}
if (isset($_GET['vr1']) && $_GET['vr1'] !='') {
	$SearchConditions .= " AND Prov_ID = {$_GET['vr1']}";
}
if (isset($_GET['vr2']) && $_GET['vr2']!='') {
	$SearchConditions .= " AND OFIDCiudad = {$_GET['vr2']}";
}
if (isset($_GET['vr3']) && $_GET['vr3']!='') {
	$SearchConditions .= " AND OFCorr = {$_GET['vr3']}";
}
if (isset($_GET['vr4']) && $_GET['vr4']!='') {
	$SearchConditions .= " AND OfDelegacion LIKE '%{$_GET['vr4']}%'";
}

if(isset($GETCTINFO[1]['OFCount']) && $GETCTINFO[1]['OFCount']>0){
	$GET = $GLOBALS['MYT']->db_array("SELECT
					OFId AS COLID,
					CorrNombre AS COL1,
					OFDIreccion AS COL2,
					OFMasDatos AS COL3,
					CiuCiudad AS COL4,
					OFObs AS COL7,
					OfHorSem AS COL8,
					OfDelegacion AS COL11
			FROM oficcorresp
				INNER JOIN ciudades ON CiuNumero = OFIDCiudad
				INNER JOIN corresponsales ON CorrNumero = OFCorr
				WHERE CorrActivo = 1 AND CiuPais = {$GETCTINFO[1]['BnfPais']} AND OFActiva = 1 {$SearchConditions}
				GROUP BY OFId
				ORDER BY OFCiudad DESC");
} else {
	$GET[0] = 0;
}
$CCOL = $GET[0];
if($CCOL > 100) {
	echo '<script charset="utf-8" type="text/javascript">alert(\'A pesquisa devolveu '.$CCOL.' agencias. Vao ser mostradas as primeiras 100.\');</script>';
	$CCOL = 100;
} elseif($CCOL > 0) {
	$CCOL = $CCOL;
}
for ($i=1;$i<=$CCOL;$i++) {
	$RSGET .= "[{$i},{$GET[$i]['COLID']},'".req_form($GET[$i]['COL1'])."','".req_form($GET[$i]['COL2'])."','".req_form($GET[$i]['COL3'])."','".req_form($GET[$i]['COL4'])."','{$GET[$i]['COL7']}','{$GET[$i]['COL8']}','".req_form($GET[$i]['COL11'])."']";
	if ($i<$CCOL) {
		$RSGET .= ',';
	}
} ?>
<div id="list_officecorr"
	style="width: 100%"></div>
<script charset="utf-8" type="text/javascript">
disableSelection(document.getElementById("list_officecorr"));
Ext.onReady(function(){

    Ext.QuickTips.init();

    var myData = [<?php echo $RSGET;?>];

    var ds = new Ext.data.Store({
        reader: new Ext.data.ArrayReader({}, [
            {name: 'id'},
	    {name: 'COLID', type: 'int'},
	    {name: 'COL1', type: 'string'},
            {name: 'COL2', type: 'string'},
            {name: 'COL3', type: 'string'},
            {name: 'COL4', type: 'string'},
            {name: 'COL8', type: 'string'},
            {name: 'COL11', type: 'string'},
	    {name: 'COL7', type: 'string'}
          ])
    });
    ds.loadData(myData);
    var colModel = new Ext.grid.ColumnModel([
        {header: "Numero", width: 80, sortable: true, locked:true, dataIndex: 'COLID'},
        {header: "Agencia", width: 150, sortable: true, dataIndex: 'COL11'},
        {header: "Correspondente", width: 100, sortable: true, locked:true, dataIndex: 'COL1'},
        {header: "Endereco", width: 150, sortable: true, dataIndex: 'COL2'},
        {header: "Outros", width: 150, sortable: true, dataIndex: 'COL3'},
        {header: "Cidade", width: 150, sortable: true, dataIndex: 'COL4'},
        {header: "Hor Semanal", width: 150, sortable: true, dataIndex: 'COL8'},
        {header: "Observacoes", width: 200, dataIndex: 'COL7'}
    ]);

   var selmode = new Ext.grid.RowSelectionModel({});
   var Console = new Ext.grid.GridPanel({

		title: 'Agencias de Pagamento',
		ds: ds,
		cm: colModel,
		sm: selmode,
		items:[new Ext.StatusBar({
				defaultText: 'Agencias encontradas: <?php echo $CCOL;?>',items:[{
				type:'button',
					width: 200,
		    		id: 'win-statusbar',
				<?php if(isset($GETCTINFO[1]) && $GETCTINFO[1]['OFCount'] > 0) { ?>
		    		text: 'Pesquisar Agencias        ',
                handler:function(){idwindow('Pesquisa de Agencias','ofcor_search','ofcor_search','Modules/XFK0WK2A/oficcorresp_search.php?RCNTR=<?php echo $GETCTINFO[1]['BnfPais'];?>',400,220);
				}<?php } else { ?>
				text: 'Nao existem agencias disponiveis neste pais.'
				<?php } ?>
				}]})],
	    height: 150,
		renderTo: 'list_officecorr',
		listeners: {
	 				rowclick: function(ds, row) {
					var record = ds.getStore().getAt(row);
  					var value = record.get('COLID');
					document.ReceiverProperties.action='_response/call.php?get=Operative/invoice&COAG='+value;
					subform('ReceiverProperties','OrderProp');
			}
		}
    });

});
</script>